package org.example.day09;

/**
 * 第三章节3.2.1.2: 方法join的基本使用 (多个线程的等待)
 */
public class MyThread2 {
    static int num1 = 0;
    static int num2 = 0;

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(){
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                num1 = 1000;
            }
        };
        Thread t2 = new Thread(){
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                num2 = 2000;
            }
        };
        long beginTime = System.currentTimeMillis();
        t1.start();
        t2.start();
        System.out.println("A: " + System.currentTimeMillis());
        //Waits for this thread to die.
        t1.join();
        System.out.println("B: " + System.currentTimeMillis());
        t2.join();
        System.out.println("C: " + System.currentTimeMillis());
        long endTime = System.currentTimeMillis();
        System.out.println("num1 = " + num1 + " ---- " + "num2 = " + num2);
        System.out.println("耗时: " + (endTime - beginTime));
    }
}
